﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using PBMessage;

namespace GameUnit
{
    public class AbilityMove : Ability
    {
        GameScene curScene;
        public enum MoveStateType
        {
            NONE,
            Idle,
            MoveLeft,
            MoveRight,
            MoveDown,
            MoveUp
        }
        MoveStateType MoveState;
        bool IsJump;

        PBMessage.CS_AbilityEvent movePacket = new PBMessage.CS_AbilityEvent();

        public override int GetTypeId()
        {
            return (int)AbilityEnum.Move;
        }
        public override void OnLoad()
        {
            curScene = Game.Scene.Cur<GameScene>();
            if (curScene.LocalPlayer.MainUnit != null && this.Unit.UnitId == curScene.LocalPlayer.MainUnit.UnitId)
            {
                curScene.InputManager.InputEvents += InputManager_InputEvents;
            }
        }

        public override void ServerUpdate(SC_ServerUpdate updatePacket)
        {
            for (int i = 0; i< updatePacket.UnitIDs.Count;i++)
            {
                if (this.Unit.UnitId == updatePacket.UnitIDs[i])
                {
                    float x = updatePacket.PosXs[i];
                    float y = updatePacket.PosYs[i];
                    this.Unit.transform.localPosition = new Vector3(x,y,0);
                    if (this.Unit.faceDir != updatePacket.FaceDirs[i])
                    {
                        this.Unit.faceDir = updatePacket.FaceDirs[i];
                        foreach (KeyValuePair<string, Actor> Item in this.Unit.Actors)
                        {
                            if (this.Unit.faceDir < 0)
                            {
                                Item.Value.ActorGameObject.transform.localRotation = Quaternion.Euler(0, this.Unit.faceDir * 90f, 0);
                            }
                            else
                            {
                                Item.Value.ActorGameObject.transform.localRotation = Quaternion.Euler(0, this.Unit.faceDir * 90f, 0);
                            }
                            
                        }
                    }
                }
            }
        }
        private void InputManager_InputEvents(KeyCode keyCode, bool isDown)
        {
            if (this.Unit.IsDie)
            {
                return;
            }
            if (isDown)
            {
                switch (keyCode)
                {
                    case KeyCode.LeftArrow:
                        ChangeMoveState(MoveStateType.MoveLeft);
                        break;
                    case KeyCode.RightArrow:
                        ChangeMoveState(MoveStateType.MoveRight);
                        break;
                    case KeyCode.DownArrow:
                        ChangeMoveState(MoveStateType.MoveDown);
                        break;
                    case KeyCode.UpArrow:
                        ChangeMoveState(MoveStateType.MoveUp);
                        break;
                    case KeyCode.X:
                        ChangeMoveState(MoveStateType.NONE, true);
                        this.Unit.CallEvent("jump","");
                        break;
                }
            }
            else
            {
                if ((MoveState == MoveStateType.MoveDown && KeyCode.DownArrow == keyCode) ||
                     (MoveState == MoveStateType.MoveUp && KeyCode.UpArrow == keyCode) ||
                     (MoveState == MoveStateType.MoveLeft && KeyCode.LeftArrow == keyCode) ||
                     (MoveState == MoveStateType.MoveRight && KeyCode.RightArrow == keyCode))
                {
                    ChangeMoveState(MoveStateType.Idle);
                }

            }
        }

        void ChangeMoveState(MoveStateType state, bool jump = false)
        {
            if (state != MoveStateType.NONE)
            {
                MoveState = state;
            }
            IsJump = jump;
            movePacket.boolArg0 = jump;
            movePacket.int64Arg0 = (int)state;
            movePacket.abilityKey =this.AbilityKey;
            movePacket.unitId = this.Unit.UnitId;
            Game.Net.NetWork.SendPacket(movePacket, PBMessageID.CS_AbilityEvent);
        }


    }
}